﻿using System.Net;
using System.Net.Http;
using Newtonsoft.Json;

namespace Best.SmartCms.Utility
{
    /// <summary>
    /// 腾讯地图
    /// </summary>
    public class TxMapHelper
    {
        /// <summary>
        /// API调用的Key，配额为20W次/日/key，与其他系统共用；
        /// 限制20次/秒
        /// </summary>
        public static readonly string Key = "IZWBZ-WTFAF-CXYJP-NL5QC-WFYQQ-2YBAI";//"MYABZ-RPFKO-XBZW7-SNE5W-ILOBS-O2FE5";

        /// <summary>
        /// 根据地址获取坐标
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public static Location GetLocation(string address)
        {
            var url = $"http://apis.map.qq.com/ws/geocoder/v1/?address={address}&key={Key}";
            using (var client = new HttpClient())
            {
                var response = client.GetAsync(url).Result;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var result = response.Content.ReadAsStringAsync().Result;
                    var data = JsonConvert.DeserializeObject<TxResponse<LocationResult>>(result);
                    if (data.Status != 0)
                    {
                        return null;
                    }

                    if (data.Result.Deviation == -1)
                    {
                        return null;
                    }

                    if (data.Result.Reliability < 5)
                    {
                        return null;
                    }

                    return data.Result.Location;
                }
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lat"></param>
        /// <param name="lng"></param>
        /// <param name="mapKey"></param>
        /// <returns></returns>
        public static AddressComponent GetAddress(string lat, string lng, string mapKey)
        {
            if (string.IsNullOrEmpty(mapKey))
            {
                mapKey = Key;
            }
            var url = $"http://apis.map.qq.com/ws/geocoder/v1/?location={lat},{lng}&get_poi=0&key={mapKey}";
            using (var client = new HttpClient())
            {
                var response = client.GetAsync(url).Result;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var result = response.Content.ReadAsStringAsync().Result;
                    var data = JsonConvert.DeserializeObject<TxResponse<LanLonToAddress>>(result);
                    if (data.Status != 0)
                    {
                        return null;
                    }
                    return data.Result.AddressComponent;
                }
                return null;
            }
        }
    }
    /// <summary>
    /// 腾讯请求返回接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class TxResponse<T>
    {
        /// <summary>
        /// 状态码， 0为正常
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// 状态说明
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 返回结构
        /// </summary>
        public T Result { get; set; }
    }

    /// <summary>
    /// 地址解析结果
    /// </summary>
    public class LocationResult
    {
        /// <summary>
        /// 详细地址——“塘苗路18号”
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 查询字符串与查询结果的文本相似度
        /// </summary>
        public decimal Similarity { get; set; }

        /// <summary>
        /// 误差距离,该值为 -1 时，说明输入地址为过于模糊，仅能精确到市区级
        /// </summary>
        public decimal Deviation { get; set; }

        /// <summary>
        /// 可信度参考：值范围 1(低可信) - 10(高可信),该值>=7时，解析结果较为准确
        /// </summary>
        public decimal Reliability { get; set; }

        /// <summary>
        /// 解析到的坐标
        /// </summary>
        public Location Location { get; set; }

        /// <summary>
        /// 解析后的地址部件
        /// </summary>
        [JsonProperty("address_components")]
        public AddressComponents AddressComponents { get; set; }
    }

    /// <summary>
    /// 坐标
    /// </summary>
    public class Location
    {
        /// <summary>
        /// 经度
        /// </summary>
        public decimal Lng { get; set; }

        /// <summary>
        /// 纬度
        /// </summary>
        public decimal Lat { get; set; }
    }

    /// <summary>
    /// 地址
    /// </summary>
    public class AddressComponents
    {
        /// <summary>
        /// 省
        /// </summary>
        public string Province { get; set; }

        /// <summary>
        /// 市
        /// </summary>
        public string City { get; set; }

        /// <summary>
        /// 区，可能为空字串
        /// </summary>
        public string District { get; set; }

        /// <summary>
        /// 街道，可能为空字串
        /// </summary>
        public string Street { get; set; }

        /// <summary>
        /// 门牌，可能为空字串
        /// </summary>
        public string StreetNumber { get; set; }
    }
    /// <summary>
    /// 经纬度解析为省市区地址
    /// </summary>
    public class LanLonToAddress
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("address_component")]
        public AddressComponent AddressComponent { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class AddressComponent
    {
        /// <summary>
        /// 省
        /// </summary>
        [JsonProperty("province")]
        public string Province { get; set; }

        /// <summary>
        /// 市
        /// </summary>
        [JsonProperty("city")]
        public string City { get; set; }

        /// <summary>
        /// 区
        /// </summary>
        [JsonProperty("district")]
        public string Area { get; set; }

        /// <summary>
        /// 街道
        /// </summary>
        [JsonProperty("street")]
        public string Street { get; set; }
        /// <summary>
        /// 详细地址
        /// </summary>
        [JsonProperty("street_number")]
        public string Address { get; set; }
    }
}
